#include "warehouse.h"
#include "log_system.h"
#include "auth_system.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



/*
** 功能：初始化系统（初始化核心模块）
** warehouse：货物链表头
** users：用户链表头
*/ 
void system_init(ItemNode **warehouse, UserNode **users)
{
	char path[256] = {0};
	char userfile[256] = {0};
	char whfile[256] = {0};
	char logfile[256] = {0};

	// 获取系统顶层目录绝对路径
	strcpy(path, getenv("WAREHOUSE_DIR"));
	// printf("path: %s\n", path);
	snprintf(userfile, 256, "%s/%s", path, "data/userlist.txt");
	snprintf(whfile, 256, "%s/%s", path, "data/warehouse.dat");
	snprintf(logfile, 256, "%s/%s", path, "data/log.txt");
	// printf("userfile: %s\n", userfile);
	// printf("whfile: %s\n", whfile);
	// printf("logfile: %s\n", logfile);
	
	// 初始化用户列表
	*users = user_list_init();
	user_load( *users, userfile );
	
	// 初始化货物列表
	*warehouse = list_init();
	file_load_data( *warehouse, whfile );
	
	// 初始化日志系统
	log_init();
	log_load_text(logfile);
}


/*
** 功能：反初始化系统
** warehouse：货物链表头
** users：用户链表头
*/ 
void system_uninit(ItemNode *warehouse, UserNode *users)
{
	char path[256] = {0};
	char userfile[256] = {0};
	char whfile[256] = {0};
	char logfile[256] = {0};

	// 获取系统顶层目录绝对路径
	strcpy(path, getenv("WAREHOUSE_DIR"));
	snprintf(userfile, 256, "%s/%s", path, "data/userlist.txt");
	snprintf(whfile, 256, "%s/%s", path, "data/warehouse.dat");
	snprintf(logfile, 256, "%s/%s", path, "data/log.txt");
	
	user_save(users, userfile);
	file_save_data(warehouse, whfile);
	log_save_text(logfile);
	
	log_cleanup();
}


/*
** 功能：个人认证
** users：用户列表
** username：输出当前登录的用户名
** 返回值：
** 		：成功 1
** 		：失败 0
*/ 
int auth(UserNode *users, char *username)
{
#define TEST
#ifdef TEST
	char user[MAX_USERNAME_LEN] = {"cd2506"};
	char pass[MAX_PASSWORD_LEN] = {"123456"};
#else	
	char user[MAX_USERNAME_LEN] = {0};
	char pass[MAX_PASSWORD_LEN] = {0};
	
	printf("用户名: ");
	scanf("%s", user);
	
	printf("密码: ");
	scanf("%s", pass);
#endif

	
	if( user_login(users, user, pass)  == 0)
	{
		printf("登录失败\n");
		return 0;
	}
	printf("登录成功\n");
	strcpy(username, user);
	return 1;
}


// 添加货物
void add_item(ItemNode *wh, const char *user)
{
	int id, qty;
	char name[50], desc[128] = {0};
	
	// 用户键盘输入
	printf("货物ID：");
	scanf("%d", &id);	
	printf("货物名称：");
	scanf("%s", name);	
	printf("数量：");
	scanf("%d", &qty);
	
	// 创建节点，添加链表
	ItemNode *new = create_node(id, name, qty);
	list_add_tail(wh, new);
	snprintf(desc, 128, "添加货物，数量：%d", qty);
	log_add(LOG_ADD_ITEM, user, id, desc);
	printf("添加成功\n");
}


// 删除货物
void del_item(ItemNode *wh, const char *user)
{
	int id;
	char desc[128] = {0};
	
	// 用户键盘输入
	printf("输入要删除的货物ID：");
	scanf("%d", &id);	
	
	// 查找该ID的货物，拿到信息组成日志说明
	ItemNode *fnode = find_by_id(wh, id);
	if(fnode == NULL)
		return ;
	snprintf(desc, 128, "删除货物：%s, 数量：%d", fnode->name, fnode->quantity);
	
	// 删除货物
	delete_by_id(wh, id);
	
	log_add(LOG_DEL_ITEM, user, id, desc);
	printf("删除成功\n");
}


// 主界面
void show_menu(const char *user)
{
	// system("clear");	//清屏
	printf("\n==============仓库管理系统============\n");
	printf("当前用户： %s\n", user);
	printf("1. 添加货物\n");
	printf("2. 删除货物\n");
	printf("3. 查看库存\n");
	printf("4. 查看日志\n");
	printf("5. 退出系统\n");	
}


int main(int argc, char *argv)
{
	// system("clear");	//清屏
	// printf("开始进入仓库管理系统\n");

	ItemNode *warehouse;
	UserNode *users;
	
	// 初始化系统
	system_init(&warehouse, &users);
	
	char username[MAX_USERNAME_LEN] = {0};
	// 用户认证
	if( auth(users, username) == 0 )
		return 0;
	log_add(LOG_LOGIN, username, 0, "登录成功,进入系统");
	
	int running = 1;
	// 进入系统
	while( running )
	{
		show_menu(username);
		printf("请选择操作：");
		
		int choice;
		scanf("%d", &choice);
		
		switch(choice)
		{
			case 1:{
				add_item(warehouse, username);
				break;
			}
			case 2:{
				del_item(warehouse, username);
				break;
			}
			case 3:{
				list_for_each(warehouse);
				break;
			}
			case 4:{
				// printf("查看日志\n");
				log_show_recent(6);
				break;
			}
			case 5:{
				printf("退出系统\n");
				// 登出日志
				log_add(LOG_LOGOUT, username, 0, "退出系统");
				running = 0;
				break;
			}
			default:
				printf("退出系统\n");
		}
	}
	
	// 系统反初始化
	system_uninit(warehouse, users);
	
	return 0;
}